package com.cssw.fyzb.busi.api.appeal;

import com.cssw.fyzb.busi.api.task.CollecTask;
import com.cssw.fyzb.core.util.DateUtil;
import com.ydn.dbframe.plugin.activerecord.Page;
import com.cssw.fyzb.core.model.vo.appeal.AppealVo;
import com.cssw.fyzb.busi.service.appeal.AppealService;
import com.cssw.fyzb.core.model.dodb.Appeal;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Function(description = "我的申述", parameters = {
        @Parameter(name = "userId", type = Type.String, description = "用户标识", required = true),
        @Parameter(name = "pageNumber", type = Type.Integer, description = "当前页，从1开始", required = true),
        @Parameter(name = "pageSize", type = Type.Integer, description = "页大小", required = true),
})
@Component
public class ListAppeal implements Action {

    private final static Logger logger = LoggerFactory.getLogger(CollecTask.class);

    @Autowired
    private AppealService appealService;

    @Override
    public Response execute(Request request) throws Exception {
        try {
            Long userId = request.getLong("userId");
            Integer pageNumber = request.getInteger("pageNumber");
            Integer pageSize = request.getInteger("pageSize");
            Page<Appeal> appeals = appealService.listAppeal(userId, pageNumber, pageSize);
            List<AppealVo> vos = new ArrayList<>();
            AppealVo vo = null;
            for (Appeal a : appeals.getList()) {
                vo = new AppealVo();
                vo.setAppeal(a.getAppeal());
                vo.setAppealAttach(a.getAppealAttach());
                vo.setAppealId(a.getAppealId());
                vo.setAppealTime(DateUtil.format(a.getAppealTime()));
                vo.setAppealType(a.getAppealType());
                vo.setContact(a.getContact());
                vo.setStatus(a.getStatus());
                vo.setFeedback(a.getFeedback());
                vo.setFeedbackOperId(a.getFeedbackOperId());
                vo.setTypeName(a.getTypeName());
                vo.setFeedbackTime(a.getFeedbackTime());
                vo.setUserId(a.getUserId());
                vos.add(vo);
            }
            return Response.success().put("list", vos).put("total", appeals.getTotalPage()).put("count", appeals.getTotalRow());
        } catch (Exception e) {
            logger.debug("", e);
            return Response.fail("操作失败");
        }
    }
}
